package com.chuang.bootplus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chuang.bootplus.base.constant.Constant;
import com.chuang.bootplus.base.utils.ApiResponse;
import com.chuang.bootplus.base.utils.BeanUtil;
import com.chuang.bootplus.entity.*;
import com.chuang.bootplus.mapper.TopicMapper;
import com.chuang.bootplus.po.topic.TopicListPO;
import com.chuang.bootplus.po.topic.TopicSavePO;
import com.chuang.bootplus.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chuang.bootplus.vo.TopicListVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-08-17
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class TopicServiceImpl extends ServiceImpl<TopicMapper, Topic> implements TopicService {

    final TopicCommentService topicCommentService;
    final TopicLikeService topicLikeService;
    final PictureService pictureService;
    final UserInfoService userInfoService;

    @Override
    public ApiResponse<Void> save(TopicSavePO po) {
        Topic topic = BeanUtil.beanA2beanB(po, Topic.class);
        save(topic);
        List<Picture> pictureList = po.getPictureList();
        for (Picture picture : pictureList) {
            picture.setParentId(topic.getId()).setTypeName(Constant.PICTURE_TYPE_TOPIC);
        }
        pictureService.saveBatch(pictureList);
        return new ApiResponse<>();
    }

    @Override
    public ApiResponse<TopicListVO> list(TopicListPO po) {
        Page<Topic> topicListPage = page(
                new Page<Topic>(po.getPageNumber(),po.getPageSize()),
                new LambdaQueryWrapper<Topic>()
                        .eq(po.getType()!=null,Topic::getType,po.getType())
                        .like(po.getTopicName()!=null,Topic::getTopicName,po.getTopicName())
                        .eq(po.getUserId()!=null,Topic::getUserId,po.getUserId())
                        .like(po.getMessage()!=null,Topic::getMessage,po.getMessage())
        );
        List<TopicListVO> topicList = BeanUtil.listA2ListB(topicListPage.getRecords(), TopicListVO.class);
        Set<Long> topicIds = topicList.stream().map(TopicListVO::getId).collect(Collectors.toSet());
        for (TopicListVO topic : topicList) {
            UserInfo userInfo = userInfoService.getById(topic.getUserId());
            if(userInfo!=null){
                topic.setUsername(userInfo.getNickName());
                topic.setUserAvatar(userInfo.getAvatarUrl());
            }
            topic.setPictureList(pictureService.list(new LambdaQueryWrapper<Picture>()
                    .eq(Picture::getTypeName,Constant.PICTURE_TYPE_TOPIC)
                    .eq(Picture::getParentId,topic.getId()))
            );
            topic.setCommentCount(topicCommentService.count(new LambdaQueryWrapper<TopicComment>().eq(TopicComment::getTopicId,topic.getId())));
            topic.setLikeCount(topicLikeService.count(new LambdaQueryWrapper<TopicLike>().eq(TopicLike::getTopicId,topic.getId())));
        }
        return new ApiResponse<>(topicList,topicListPage.getTotal());
    }

}
